package org.metasketch.simulator.tools.transactions;

import java.util.ArrayList;
import java.util.StringTokenizer;

import org.metasketch.demo.model.DEMOModel;
import org.metasketch.simulator.resources.ResourceBank;
import org.metasketch.simulator.tools.Registry;
import org.metasketch.simulator.tools.channels.dynamic.StepChannel;
import org.metasketch.simulator.tools.generic.GenericInfoValidator;
import org.metasketch.simulator.transformation.PetriNetSimulationPackage;

public class StepDefinitionValidator {

	public static void validate(StepDefinition step,
			String parentTransactionAlias, DEMOModel model,
			PetriNetSimulationPackage simulationPackage) throws Exception {

		Registry reg = simulationPackage.getRegistry();
		ResourceBank bank = simulationPackage.getResources();

		setStepNames(step);

		// TODO validate staff names as well...compared to the transaction petri
		// resources available to see if they apply or not...

		// finally validate variables and resource requirements and productions
		GenericInfoValidator.validate(step, reg, bank);

	}

	protected static void setStepNames(StepDefinition step) {
		
		step.setStepNames(new ArrayList<StepName>());
		

		step.setStepNames(new ArrayList<StepName>());

		// Validate steps names
		StringTokenizer st = new StringTokenizer(step.getSteps());
		while (st.hasMoreTokens()) {
			String token = st.nextToken();
			StepName stepEnum = StepName.valueOf(token);
			step.getStepNames().add(stepEnum);
		}
	}

	public static void setStepNames(StepChannel step) {
		step.setStepNames(new ArrayList<StepName>());
		
		// Validate steps names
		StringTokenizer st = new StringTokenizer(step.getSteps());
		while (st.hasMoreTokens()) {
			String token = st.nextToken();
			StepName stepEnum = StepName.valueOf(token);
			step.getStepNames().add(stepEnum);
		}
	}

}
